package com.chixing.ih.module.task.service.impl;

import com.chixing.ih.module.programmer.mapper.ProgrammerMapper;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.dto.ProgrammerEntiretyDTO;
import com.chixing.ih.module.system.pojo.ToEmail;
import com.chixing.ih.module.task.mapper.AcceptTaskMapper;
import com.chixing.ih.module.task.mapper.OrderMapper;
import com.chixing.ih.module.task.mapper.TaskMapper;
import com.chixing.ih.module.task.pojo.AcceptTask;
import com.chixing.ih.module.task.pojo.DTO.OrderDTO;
import com.chixing.ih.module.task.pojo.DTO.OrderListDTO;
import com.chixing.ih.module.task.pojo.DTO.ProCommentDTO;
import com.chixing.ih.module.task.pojo.DTO.TaskShowDTO;
import com.chixing.ih.module.task.pojo.Order;
import com.chixing.ih.module.task.pojo.Task;
import com.chixing.ih.module.task.service.IOrderService;
import com.chixing.ih.module.task.service.ITaskService;
import com.chixing.ih.status.ReceiveTaskEnum;
import com.chixing.ih.util.EmptyUtil;
import com.chixing.ih.util.GenerateOrderNoUtil;
import com.chixing.ih.util.MailUtil;
import com.chixing.ih.util.PageRequest;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.1  2020/9/26 16:53
 */
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProgrammerMapper programmerMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskServiceImpl taskService;

    @Autowired
    private ITaskService iTaskService;

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private MailUtil mailUtil;

    @Autowired
    private AcceptTaskMapper acceptTaskMapper;


    @Override
    public int consentApplication(Order order) {
        AcceptTask acceptTask = new AcceptTask();
        acceptTask.setTaskId(order.getTaskId());
        acceptTask.setReceiveTaskUserId(order.getReceiveTaskUserId());
        acceptTask.setReceiveTaskStatus(ReceiveTaskEnum.ONE.status);
        return acceptTaskMapper.updateOne(acceptTask);
    }

    @Override
    public int insertOne(Order order) {
        order.setUpdateTime(new Date());
        return orderMapper.insertOne(order);
    }

    @Override
    public Order getOne(Long orderId) {
        return orderMapper.getOne(orderId);
    }

    @Override
    public Order findOne(Long taskId) {
        return orderMapper.findOne(taskId);
    }

    @Override
    public List<OrderListDTO> findAll(PageRequest pageRequest) {

        if (EmptyUtil.isEmpty(pageRequest.getPageNum())) {
            pageRequest.setPageNum(1);
        }

        if (EmptyUtil.isEmpty(pageRequest.getPageSize())) {
            pageRequest.setPageSize(10);
        }

        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());

        List<OrderListDTO> list = orderMapper.findAll();

        for (int i = 0; i < list.size(); i++) {
            Programmer receiverUser = programmerMapper.getOne(list.get(i).getOrder().getReceiveTaskUserId());
            Programmer sendUser = programmerMapper.getOne(list.get(i).getOrder().getSendTaskUserId());
            TaskShowDTO task = taskMapper.findOne(list.get(i).getOrder().getTaskId());
            list.get(i).setTask(task);
            list.get(i).setSendUser(sendUser);
            list.get(i).setReceiveUser(receiverUser);
        }

        return list;
    }

    @Override
    public List<TaskShowDTO> findCompletedTaskSuccess(Long userId) {
        List<Order> list = orderMapper.findCompletedTaskSuccess(userId);
        List<TaskShowDTO> tasks = new LinkedList<>();

        for (int i = 0; i < list.size(); i++) {
            TaskShowDTO task = taskService.findOne(list.get(i).getTaskId());
            if (null != task) {
                tasks.add(task);
            }
        }
        return tasks;
    }

    @Override
    public List<TaskShowDTO> findCompletedTaskFailure(Long userId) {
        List<Order> list = orderMapper.findCompletedTaskFailure(userId);
        List<TaskShowDTO> tasks = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            TaskShowDTO task = taskService.findOne(list.get(i).getTaskId());
            if (null != task) {
                tasks.add(task);
            }
        }
        return tasks;
    }

    @Override
    public List<TaskShowDTO> findCompletedEmploymentSuccess(Long userId) {
        List<Order> list = orderMapper.findCompletedEmploymentSuccess(userId);
        List<TaskShowDTO> tasks = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            TaskShowDTO task = taskService.findOne(list.get(i).getTaskId());
            if (null != task) {
                task.setOrderId(list.get(i).getOrderId());
                task.setPaymentStatus(list.get(i).getPaymentStatus());
                task.setReceiveName(list.get(i).getReceiveName());
                task.setReceiveImageUrl(list.get(i).getReceiveImageUrl());
                tasks.add(task);
            }
        }
        return tasks;
    }

    @Override
    public List<TaskShowDTO> findCompletedEmploymentFailure(Long userId) {
        List<Order> list = orderMapper.findCompletedEmploymentFailure(userId);
        List<TaskShowDTO> tasks = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            TaskShowDTO task = taskService.findOne(list.get(i).getTaskId());
            if (null != task) {
                task.setOrderId(list.get(i).getOrderId());
                task.setPaymentStatus(list.get(i).getPaymentStatus());
                task.setReceiveName(list.get(i).getReceiveName());
                task.setReceiveImageUrl(list.get(i).getReceiveImageUrl());
                tasks.add(task);
            }
        }
        return tasks;
    }


    @Override
    public List<TaskShowDTO> findProgressTask(Long userId) {
        List<Order> list = orderMapper.findProgressTask(userId);
        List<TaskShowDTO> tasks = new LinkedList<>();
        for (int i = 0; i < list.size(); i++) {
            TaskShowDTO task = taskService.findOne(list.get(i).getTaskId());
            if (null != task) {
                tasks.add(task);
            }
        }
        return tasks;
    }

    @Override
    public List<TaskShowDTO> findProgressEmployment(Long userId) {
        List<TaskShowDTO> tasks = orderMapper.findProgressEmployment(userId);
        return tasks;
    }

    @Override
    public int applicationSettlement(Order order) {
        order.setOrderStatus(1);
        order.setOrderId(findOne(order.getTaskId()).getOrderId());
        return orderMapper.applicationSettlement(order);
    }

    @Override
    public int applyRefund(Order order) {
        order.setOrderStatus(3);
        order.setOrderEndTime(new Date());
        order.setOrderId(findOne(order.getTaskId()).getOrderId());
        return orderMapper.applyRefund(order);
    }

    @Override
    public long orderTotalNumber() {
        return orderMapper.orderTotalNumber();
    }

    @Override
    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    @Override
    public ProCommentDTO getPendingComOrder(Long orderId) {
        ProCommentDTO pendingComOrder = orderMapper.getPendingComOrder(orderId);
        return pendingComOrder;
    }

    @Override
    public OrderDTO getOneOrder(Long orderId) {
        return orderMapper.getOneOrder(orderId);
    }
}
